package hvzt.minimap.activities.mapItems;

import iface.HvZUser;

import java.util.ArrayList;
import java.util.Collection;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;

/**
 * An Extension of ItemizedOverlay that can handle the addition of HvZUsers.
 * OnTap pops up an alert dialog on the context passed at construction.
 * @author JakeS
 *
 */
public final class ZMapOverlay extends ItemizedOverlay<PlayerOverlay>{
	private ArrayList<PlayerOverlay> mOverlays = new ArrayList<PlayerOverlay>();
	private Context mContext;

	/**
	 * Constructs a new ZMapOverlay whose items are represented by the given Drawable.
	 * @param defaultMarker the Drawable to mark the contained PersonOverlays
	 */
	public ZMapOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		populate();
	}

	/**
	 * Constructs a new ZMapOverlay whose items are represented by the given Drawable,
	 * and is tied to the given context.
	 * @param defaultMarker the Drawable to mark the contained PersonOverlays
	 * @param context the Context to be tied to
	 */
	public ZMapOverlay(Drawable defaultMarker, Context context) {
		// changed to this from super
		super(boundCenterBottom(defaultMarker));
		mContext = context;// the parent for dialog windows
		populate();
	}
	
	/**
	 * Adds the given PersonOverlay item to the list of overlay items.
	 * Subsequent calls to getItem(n) will return the nth added item (starting at 0).
	 * @param overlay the PersonOverlay to add
	 */
	private void addOverlay(PlayerOverlay overlay) {
		mOverlays.add(overlay);
		populate();
	}
	
	/**
	 * Adds a PersonOverlay constructed from the given HvZUser.
	 * Construction of a PersonOverlay from an HvZUser is defined in
	 * PersonOverlay. 
	 * @param user the HvZUser to use to construct a PersonOverlay
	 */
	public void addUser(HvZUser user) {
		addOverlay(new PlayerOverlay(user));
	}
	
	/**
	 * Replaces all current PersonOverlays contained with overlays made from
	 * the given collection of HvZUsers
	 * @param users the users to set as PlayerOverlays
	 */
	public void setUsers(Collection<HvZUser> users){
		mOverlays = new ArrayList<PlayerOverlay>(users.size());
		for(HvZUser user : users){
			mOverlays.add(new PlayerOverlay(user));
		}
		populate();
	}

	/**
	 * Used by superclass when populate is called
	 */
	protected PlayerOverlay createItem(int i) {
		return mOverlays.get(i);
	}

	/**
	 * @return the contained number of PersonOverlays
	 */
	public int size() {
		return mOverlays.size();
	}

	/**
	 * Indicates when the given overlay item was tapped, does stuff to it
	 */
	protected boolean onTap(int index) {
		// brings up a dialog
		PlayerOverlay item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}
}
